package com.zmn.oms.dubbo.impl.zmn.attachment;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.dubbo.dto.common.work.OrderAttachmentDRO;
import com.zmn.oms.dubbo.interfaces.zmn.normal.attachment.OrderAttachmentRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.entity.orderattachment.OrderAttachment;
import com.zmn.oms.services.interfaces.orderattachment.OrderAttachmentService;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author sunlife
 * @date:2020/11/11 7:19 下午
 * description:
 */
@Service(loadbalance = "roundrobin", timeout = 5000, retries = 2, version = DubboConsts.INTERFACE_VERSION, validation = "false")
public class OrderAttachmentRemoteServiceImpl implements OrderAttachmentRemoteService {

    @Resource
    OrderAttachmentService orderAttachmentService;

    @Override
    public ResponseDTO<Integer> getAttachementResultByOrderIdAndType(@Valid @NotNull(message = "工单号不能为空") Long workId, @Valid @NotNull(message = "附件类型不能为空") Integer type) {
        AtomicReference<Integer> result = new AtomicReference<>(GlobalConsts.NONE);
        orderAttachmentService.listAttachmentByOrderId(workId).stream().filter(atta -> Objects.equals(atta.getType(), type)).findFirst().ifPresent(atta -> result.set(atta.getReviewStatus()));
        return ResponseDTO.success(result.get());
    }

    @Override
    public ResponseDTO<List<OrderAttachmentDRO>> getAttachementByOrderIdAndType(@Valid @NotNull(message = "工单号不能为空") Long workId, Integer type) {
        List<OrderAttachment> attachments = orderAttachmentService.listAttachmentByOrderId(workId);
        if (Objects.nonNull(type)) {
            attachments.removeIf(atta -> !Objects.equals(atta.getType(), type));
        }
        return ResponseDTO.success(BeanMapper.mapList(attachments, OrderAttachmentDRO.class));
    }
}
